2-sat constructive algorithms greedy string suffix structures strings *800

Please click on ads to support us..

Python Code:

for _ in range(int(input())):
    n, m = map(int, input().split())
    an = list(map(int, input().split()))
    res = ['B' for _ in range(m)]
    for i in range(n):
        a, b = min(an[i]-1, m-an[i]), max(an[i]-1, m-an[i])
        if res[a] == 'B':
            res[a] = 'A'
        elif res[b] == 'B':
            res[b] = 'A'
    print("".join(i for i in res))

C++ Code:

#include <iostream>
#include <queue>
#include <algorithm>

using namespace std;

int64_t mod = 1000000007;

int pos[50];
char s[51];

void solve()
{
    int n, m;

    cin >> n >> m;

    for (int i = 0; i < m; ++i)
    {
        pos[i] = 0;
    }

    int a;
    for (int i = 0; i < n; ++i)
    {
        cin >> a;
        a--;
        if (m-1-a < a)
        {
            a = m-1-a;
        }

        if (pos[a] == 0)
        {
            pos[a]++;
        }
        else
        {
            pos[m-1-a]++;
        }
    }

    for (int i = 0; i < m; ++i)
    {
        if (pos[i])
        {
            cout << "A";
        }
        else
        {
            cout << "B";
        }
    }

    cout << "\n";
}

int main()
{
    //freopen("input.txt", "r", stdin);
    int t = 1;

    cin >> t;

    for (int i = 0; i < t; ++i)
    {
        solve();
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts
Zoos
Build a graph
Almost correct bracket sequence
Count of integers
Differences of the permutations
Doctor's Secret
Back to School
I am Easy
Teddy and Tweety
Partitioning binary strings
Special sets
Smallest chosen word
Going to office
Color the boxes
Missing numbers
Maximum sum
13 Reasons Why
Friend's Relationship
Health of a person
Divisibility
A. Movement
Numbers in a matrix
Sequences
Split houses